﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;
using System.Web;

namespace WebBotPlugin
{
    class WebBot
    {
        public void Send(string url)
        {
            Thread thread = new Thread(new ParameterizedThreadStart(SendURL));
            thread.Start(url);
        }

        public void SendURL(object url)
        {
            string strResult;
            HttpWebRequest reqFirstPage = (HttpWebRequest)WebRequest.Create((string)url);
            HttpWebResponse resURL = (HttpWebResponse)reqFirstPage.GetResponse();
            StreamReader srURL = new StreamReader(resURL.GetResponseStream());
            strResult = srURL.ReadToEnd();
        }

        public string SendReturnURL(object url)
        {
            string strResult = "";
            HttpWebRequest reqFirstPage = (HttpWebRequest)WebRequest.Create((string)url);
            HttpWebResponse resURL = (HttpWebResponse)reqFirstPage.GetResponse();
            StreamReader srURL = new StreamReader(resURL.GetResponseStream());
            strResult = srURL.ReadToEnd();

            return strResult;
        }

        public string TimeStamp(string format = "h:mm:ss tt")
        {
            DateTime parsedDate = DateTime.Now;
            string timeStamp = parsedDate.ToString(format);
            return timeStamp;
        }

        public string ParseStatstring(string product, string statstring, string method)
        {
            string[] stats = statstring.Split(new char[] { ' ' });

            if (method == "UserJoin")
            {
                switch (product)
                {
                    case "STAR":
                    case "SEXP":
                        return " with " + stats[3] + " wins";
                    case "W2BN":
                        return " with " + stats[3] + " wins";
                    default:
                        return null;
                }
            }

            if (method == "ClanTag")
            {
                switch (product)
                {
                    case "WAR3":
                    case "W3XP":
                        if (stats.Length == 4)
                        {
                            return ReverseString(stats[3]);
                        }
                        break;
                    default:
                        return null;
                }
            }

            return null;
        }

        public string ReverseString(string s)
        {
            char[] arr = s.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);
        }
    }
}
